home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 15.0 KB | 498 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: ClockFac.cpp
- // Release Version: $ 1.0d1 $
- //
- // Author: Lonnie Millett
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef CLOCKFAC_H
- #include "ClockFac.h"
- #endif
-
- #ifndef CLOCKFRA_H
- #include "ClockFra.h"
- #endif
-
- #ifndef CLOCKPAR_H
- #include "ClockPar.h"
- #endif
-
- // ----- FrameWork Includes -----
-
- #ifndef FWUTIL_H
- #include <FWUtil.h>
- #endif
-
- // ----- Graphic Includes -----
-
- #ifndef FWOVLSHP_H
- #include "FWOvlShp.h"
- #endif
-
- #ifndef FWTXTSHP_H
- #include "FWTxtShp.h"
- #endif
-
- #ifndef FWLINSHP_H
- #include "FWLinShp.h"
- #endif
-
- #ifndef FWCOLOR_H
- #include "FWColor.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _FACET_
- #include <Facet.h>
- #endif
-
- #ifndef _FRAME_
- #include <Frame.h>
- #endif
-
- #ifndef _SHAPE_
- #include <Shape.h>
- #endif
-
- #ifndef _DRAGDROP_
- #include <DragDrop.h>
- #endif
-
- #ifndef _DGITMITR_
- #include <DgItmItr.h>
- #endif
-
- #ifndef _STDPROPS_
- #include <StdProps.h>
- #endif
-
- #ifndef _STDTYPES_
- #include <StdTypes.h>
- #endif
-
- #ifndef _STORAGEU_
- #include <StorageU.h>
- #endif
-
- #ifndef _XMPSESSN_
- #include <XMPSessM.h>
- #endif
-
- #ifndef _TRANSLAT_
- #include <Translat.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__STDTYPES__)
- #include <StdTypes.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
- #include <Quickdraw.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__FONTS__)
- #include <Fonts.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__TEXTEDIT__)
- #include <TextEdit.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
- #include <Script.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__PACKAGES__)
- #include <Packages.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
- #include <Resources.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
- #include <ToolUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__OSUTILS__)
- #include <OSUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
- #include <math routines.h>
- #endif
-
- #pragma segment cclockfacet
-
- //----------------------------------------------------------------------------------------
- // The new math libraries for the universal headers have bugs so we're just including
- // definitions from the old headers and linking with the old library till the numerics
- // guys get the bugs worked out.
- //
- //extern "C" {
- // extended rint(extended x);
- // extended sin(extended x);
- // extended cos(extended x);
- // extended pi();
- //}
- //
- //#ifndef __FP__
- //#include <Fp.h>
- //#endif
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::CClockFacet
- //----------------------------------------------------------------------------------------
- CClockFacet::CClockFacet()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::~CClockFrame
- //----------------------------------------------------------------------------------------
- CClockFacet::~CClockFacet()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::InitClockFacet
- //----------------------------------------------------------------------------------------
- void CClockFacet::InitClockFacet(XMPFacet* xmpFacet, CClockFrame* clockFrame)
- {
- this->InitFacet(xmpFacet);
- fClockFrame = clockFrame;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::Draw
- //----------------------------------------------------------------------------------------
- void CClockFacet::Draw(FW_CGraphicContext *gc)
- {
- FW_UNUSED(gc);
-
- unsigned long tickCount = fClockFrame->GetClockPart()->GetLastTime();
-
- if (fClockFrame->GetClockPart()->GetClockType() == kAnalogClock)
- {
- this->DrawClockFace(gc);
- this->DrawClockHands(gc, tickCount);
- }
- else
- UpdateDigitalClock(gc, tickCount);
- }
-
- //------------------------------------------------------------------------------
- // CClockFacet::DragEnter
- //------------------------------------------------------------------------------
- void CClockFacet::DragEnter(XMPDragItemIterator* dragInfo, const FW_CPoint& where)
- {
- XMPTranslation *translate = GetFrame()->GetPart()->GetSession()->GetTranslation();
- XMPType applePICT = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
- XMPType applesnd = translate->GetISOTypeFromPlatformType('snd ', kXMPPlatformDataType);
-
- FW_CFacet::DragEnter(dragInfo, where);
-
- if (!this->GetCanAcceptDrop())
- {
- XMPStorageUnit *dragSU;
-
- for (dragSU = dragInfo->First(); dragSU; dragSU = dragInfo->Next())
- if (dragSU->Exists(kXMPPropContents, applePICT, 0) ||
- dragSU->Exists(kXMPPropContents, applesnd, 0))
- this->SetCanAcceptDrop(TRUE);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::DrawClockFace
- //----------------------------------------------------------------------------------------
- void CClockFacet::DrawClockFace(FW_CGraphicContext *gc)
- {
- FW_CPoint centerPoint;
- FW_CPoint newPoint;
- FW_CPoint tickPoint;
- Fixed radius;
-
- FW_CRect clockRect;
- fClockFrame->GetFrameShapeBounds(&clockRect);
-
- centerPoint.x = (clockRect.right - clockRect.left) / 2;
- centerPoint.y = (clockRect.bottom - clockRect.top) / 2;
- radius = centerPoint.x;
-
- FW_COvalShape ovalShape(clockRect);
-
- // ----- Erase under the clock -----
- ovalShape->SetForeColor(FW_kRGBWhite);
- ovalShape->Draw(gc);
-
- PicHandle picHandle = fClockFrame->GetClockPart()->GetBackgroundPICT();
- FW_SPlatformRect qdRect = gc->AsPlatformRect(clockRect);
- if (picHandle)
- ::DrawPicture(picHandle, &qdRect);
-
- // ----- Frame it -----
- ovalShape->SetForeColor(FW_kRGBBlack);
- ovalShape->SetPenSize(ff(2));
- ovalShape->SetShapeFill(FW_kFramed);
- ovalShape->Draw(gc);
-
- // ----- Set the style's font -----
-
- Str255 clockFaceString;
- FW_CAcquireASLMResourceAccess aq;
- ::GetIndString(clockFaceString, kClockPartStrings, kClockFaceString);
-
- FW_CTextShape textShape(clockFaceString[0], (char*)&clockFaceString[1], FW_CPoint(0,0));
- textShape->SetFontName(FW_kHelvetica);
- textShape->SetFontStyle(FW_kItalic);
- textShape->SetFontSize(radius / 10);
-
- FW_CRect textBounds = textShape->GetShapeBounds();
- FW_CPoint pos(((clockRect.right - clockRect.left) - (textBounds.right - textBounds.left)) / 2, (clockRect.bottom - clockRect.top) / 4);
- textShape->SetPosition(pos);
- textShape->Draw(gc);
-
- // Use the symmetry of the clock face to speed up the calculations for drawing the face
- // We only need to calculate points for 45 degrees of the circle. The remaining points can
- // be inferred from these points
- short angle = 0;
- short fiveMinute = 0;
-
- while (angle < 45)
- {
- // convert angle to radians
- Fixed radians = ::FixDiv(fl(3.1416) * (90-angle), ff(180));
- Fixed cosRadian = ::Frac2Fix(::FracCos(radians));
- Fixed sinRadian = ::Frac2Fix(::FracSin(radians));
-
- newPoint.x = ::FixMul(radius, cosRadian) + centerPoint.x;
- newPoint.y = ::FixMul(radius, sinRadian) + centerPoint.y;
-
- if (fiveMinute == 0)
- {
- Fixed hourTick = radius - (radius / 10);
- tickPoint.x = ::FixMul(hourTick, cosRadian) + centerPoint.x;
- tickPoint.y = ::FixMul(hourTick, sinRadian) + centerPoint.y;
- fiveMinute = 4;
- }
- else
- {
- Fixed minuteTick = radius - (radius / 20);
- tickPoint.x = ::FixMul(minuteTick, cosRadian) + centerPoint.x;
- tickPoint.y = ::FixMul(minuteTick, sinRadian) + centerPoint.y;
- fiveMinute--;
- }
-
- FW_CLineShape lineShape(FW_CPoint(newPoint.x - ff(1), newPoint.y - ff(1)),
- FW_CPoint(tickPoint.x - ff(1), tickPoint.y - ff(1)));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(clockRect.right - newPoint.x, newPoint.y - ff(1)));
- lineShape->SetLineEnd(FW_CPoint(clockRect.right - tickPoint.x, tickPoint.y - ff(1)));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(newPoint.x - ff(1), clockRect.bottom - newPoint.y));
- lineShape->SetLineEnd(FW_CPoint(tickPoint.x - ff(1), clockRect.bottom - tickPoint.y));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(clockRect.right - newPoint.x, clockRect.bottom - newPoint.y));
- lineShape->SetLineEnd(FW_CPoint(clockRect.right - tickPoint.x, clockRect.bottom - tickPoint.y));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(newPoint.y - ff(1), newPoint.x - ff(1)));
- lineShape->SetLineEnd(FW_CPoint(tickPoint.y - ff(1), tickPoint.x - ff(1)));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(clockRect.bottom - newPoint.y, newPoint.x - ff(1)));
- lineShape->SetLineEnd(FW_CPoint(clockRect.bottom - tickPoint.y, tickPoint.x - ff(1)));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(newPoint.y - ff(1), clockRect.right - newPoint.x));
- lineShape->SetLineEnd(FW_CPoint(tickPoint.y - ff(1), clockRect.right - tickPoint.x));
- lineShape->Draw(gc);
-
- lineShape->SetLineStart(FW_CPoint(clockRect.bottom - newPoint.y, clockRect.right - newPoint.x));
- lineShape->SetLineEnd(FW_CPoint(clockRect.bottom - tickPoint.y, clockRect.right - tickPoint.x));
- lineShape->Draw(gc);
-
- angle += 6;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::DrawClockHands
- //----------------------------------------------------------------------------------------
- void CClockFacet::DrawClockHands(FW_CGraphicContext *gc, unsigned long tickCount)
- {
- FW_CPoint centerPoint;
- DateTimeRec time;
-
- FW_CRect clockRect;
- fClockFrame->GetFrameShapeBounds(&clockRect);
-
- centerPoint.x = (clockRect.right - clockRect.left) / 2;
- centerPoint.y = (clockRect.bottom - clockRect.top) / 2;
-
- ::Secs2Date(tickCount, &time);
-
- this->DrawSecondHand(gc, clockRect, centerPoint, ff(time.second));
- this->DrawMinuteHand(gc, clockRect, centerPoint, ff(time.minute));
- this->DrawHourHand(gc, clockRect, centerPoint, ff(time.hour), ff(time.minute));
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::CalcPoint
- //----------------------------------------------------------------------------------------
-
- void CClockFacet::CalcPoint(FW_CPoint* point, XMPCoordinate radius, const XMPPoint& center, const FW_CRect& frame, Fixed angle)
- {
- point->x = ::FixMul(radius, ::Frac2Fix(::FracSin(angle))) + center.x - ff(1);
- point->y = frame.bottom - (::FixMul(radius, ::Frac2Fix(::FracCos(angle))) + center.y);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::DrawHourHand
- //----------------------------------------------------------------------------------------
- void CClockFacet::DrawHourHand(FW_CGraphicContext *gc,
- const FW_CRect& clockRect,
- const FW_CPoint& centerPoint,
- Fixed hour,
- Fixed minute)
- {
- FW_CPoint newPoint;
- Fixed hourRadius = centerPoint.x / 2; // Make the hour hand short and stubby
-
- if (hour > ff(11))
- hour -= ff(12);
-
- // Divide the clock face into degrees. Hour hand falls on every five minutes or every 30 degrees
- Fixed radians = ::FixDiv(((hour * 30) + (minute / 2)), ::FixDiv(ff(180), fl(3.1416)));
-
- CalcPoint(&newPoint, hourRadius, centerPoint, clockRect, radians);
-
- FW_CLineShape line(centerPoint, newPoint);
- line->SetPenSize(ff(3));
- line->SetTransferMode(FW_kXOr);
- line->Draw(gc);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::DrawMinuteHand
- //----------------------------------------------------------------------------------------
- void CClockFacet::DrawMinuteHand(FW_CGraphicContext *gc,
- const FW_CRect& clockRect,
- const FW_CPoint& centerPoint,
- Fixed minute)
- {
- FW_CPoint newPoint;
- Fixed minuteRadius = centerPoint.x - (centerPoint.x / 20); // Make the minute hand longer than the hour hand
-
- // Divide the clock face into degrees. (360 degrees divided by 60 minutes is 6 degrees per minute)
- Fixed radians = ::FixDiv(minute * 6, ::FixDiv(ff(180), fl(3.1416)));
-
- CalcPoint(&newPoint, minuteRadius, centerPoint, clockRect, radians);
-
- FW_CLineShape line(centerPoint, newPoint);
- line->SetPenSize(ff(2));
- line->SetTransferMode(FW_kXOr);
- line->Draw(gc);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::DrawSecondHand
- //----------------------------------------------------------------------------------------
- void CClockFacet::DrawSecondHand(FW_CGraphicContext *gc,
- const FW_CRect& clockRect,
- const FW_CPoint& centerPoint,
- Fixed second)
- {
- FW_CPoint newPoint;
- Fixed secondRadius = centerPoint.x - (centerPoint.x / 40); // A nice sweeping second hand
-
- // Divide the clock face into degrees. (360 degrees divided by 60 seconds is 6 degrees per second)
- Fixed radians = ::FixDiv(second * 6, ::FixDiv(ff(180), fl(3.1416)));
-
- CalcPoint(&newPoint, secondRadius, centerPoint, clockRect, radians);
-
- FW_CLineShape line(centerPoint, newPoint);
- line->SetTransferMode(FW_kXOr);
- line->Draw(gc);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::UpdateAnalogClock
- //----------------------------------------------------------------------------------------
- void CClockFacet::UpdateAnalogClock(FW_CGraphicContext *gc, unsigned long tickCount)
- {
- FW_CPoint centerPoint;
- DateTimeRec time;
- DateTimeRec lastTime;
-
- ::GetDateTime(&tickCount);
- ::Secs2Date(tickCount, &time);
- ::Secs2Date(fClockFrame->GetClockPart()->GetLastTime(), &lastTime);
-
- FW_CRect clockRect;
- fClockFrame->GetFrameShapeBounds(&clockRect);
-
- centerPoint.x = (clockRect.right - clockRect.left) / 2;
- centerPoint.y = (clockRect.bottom - clockRect.top) / 2;
-
- // Since we're called every second go ahead and update the second hand
- this->DrawSecondHand(gc, clockRect, centerPoint, ff(lastTime.second));
- this->DrawSecondHand(gc, clockRect, centerPoint, ff(time.second));
-
- if (lastTime.minute != time.minute)
- {
- this->DrawMinuteHand(gc, clockRect, centerPoint, ff(lastTime.minute));
- this->DrawMinuteHand(gc, clockRect, centerPoint, ff(time.minute));
-
- this->DrawHourHand(gc, clockRect, centerPoint, ff(lastTime.hour), ff(lastTime.minute));
- this->DrawHourHand(gc, clockRect, centerPoint, ff(time.hour), ff(time.minute));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockFacet::UpdateDigitalClock
- //----------------------------------------------------------------------------------------
- void CClockFacet::UpdateDigitalClock(FW_CGraphicContext *gc, unsigned long tickCount)
- {
- Str255 str;
- ::IUTimeString(tickCount, TRUE, str);
-
- FW_CString32 time;
- time.ReplaceAll(str);
-
- FW_CRect clockRect;
- fClockFrame->GetFrameShapeBounds(&clockRect);
-
- FW_CRectShape rectShape(clockRect);
-
- rectShape->SetForeColor(FW_kRGBWhite);
- rectShape->Draw(gc);
-
- rectShape->SetShapeFill(FW_kFramed);
- rectShape->SetForeColor(FW_kRGBBlack);
- rectShape->Draw(gc);
-
- FW_CTextShape textShape(time);
- textShape->SetFontName(FW_kSystemFont);
- textShape->SetFontSize(ff(12));
- textShape->Draw(gc, clockRect, FW_kFlushCenter);
- }
-
-